package com.yhsimon.realm.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsimon.realm.member.constant.BeanUsageCodeEnum;
import com.yhsimon.realm.member.entity.user.BeanBillEntity;
import com.yhsimon.realm.member.entity.user.BeanEntity;
import com.yhsimon.realm.member.entity.user.LoginRecordEntity;
import com.yhsimon.realm.member.mapper.BeanBillMapper;
import com.yhsimon.realm.member.service.BeanService;
import com.yhsimon.realm.member.mapper.BeanMapper;
import com.yhsimon.realm.member.vo.user.BeanBillVo;
import com.yhsimon.realm.member.vo.user.BeanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19819
 * @description 针对表【ums_bean(用户仙豆表)】的数据库操作Service实现
 * @createDate 2024-05-06 10:19:13
 */
@Service
public class BeanServiceImpl extends ServiceImpl<BeanMapper, BeanEntity>
        implements BeanService {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private BeanBillMapper beanBillMapper;

    @Override
    public BeanVo getBeanByUserId(long userId) {
        BeanEntity beanEntity = beanMapper.selectOne(new QueryWrapper<BeanEntity>().eq("user_id", userId).eq("is_deleted", 0));
        BeanVo beanVo = new BeanVo();
        BeanUtils.copyProperties(beanEntity, beanVo);
        return beanVo;
    }

    @Override
    public List<BeanBillVo> getBeanBillByUserId(int currentPage, int size, long userId) {
        Page<BeanBillEntity> page = new Page<>(currentPage, size);
        List<BeanBillEntity> beanBillEntityList = beanBillMapper.selectList(page, new QueryWrapper<BeanBillEntity>().eq("user_id", userId).orderByDesc("create_time"));
        List<BeanBillVo> beanBillVoList = new ArrayList<>();
        for (BeanBillEntity beanBillEntity : beanBillEntityList) {
            BeanBillVo beanBillVo = new BeanBillVo();
            BeanUtils.copyProperties(beanBillEntity, beanBillVo);
            beanBillVo.setUsageModeName(BeanUsageCodeEnum.getUsageModeName(beanBillEntity.getUsageMode()));
            beanBillVoList.add(beanBillVo);
        }
        return beanBillVoList;
    }
}




